package com.meilele.controller.czh;

import com.meilele.biz.czh.CompanyBiz;
import com.meilele.biz.hongdian.ThiCaseBiz;
import com.meilele.entity.czh.FiveCompany;
import com.meilele.util.czh.Constants;
import com.meilele.util.czh.MyApplicationContext;
import com.meilele.util.zhengchao.Contants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/fiveCompany")
public class FiveCompanyController {
    @Resource
    private CompanyBiz companyBiz;
    @Resource
    private ThiCaseBiz thiCaseBiz;
    @RequestMapping("/list")
    public String list(String cityName, int pageNum, Model model, HttpServletRequest request){
        byte[] b= new byte[0];//用tomcat的格式（iso-8859-1）方式去读。
        try {
//            String cityName1=request.getParameter("cityName");
            b = cityName.getBytes("ISO-8859-1");
            String str=new String(b,"utf-8");
            List<FiveCompany>list=companyBiz.selectFiveCompanyCity(str,pageNum);
            int totalCount=companyBiz.findCount(str);
            double totalPage=totalCount*1.0/ Constants.PAGE_SIZE;
            int total=(int) Math.ceil(totalPage);
            String city=list.get(0).getCompanyCity();
            model.addAttribute("city",city);
            model.addAttribute("list",list);
            model.addAttribute("total",total);
            model.addAttribute("pageNum",pageNum);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return "/fiveCompany/list";
    }
    @RequestMapping("/preadd")
    public String preadd(){
        return "/fiveCompany/add";
    }
    @RequestMapping("/add")
    public String add(FiveCompany fiveCompany,MultipartHttpServletRequest request)throws Exception{
        MultipartFile file = request.getFile("photo");
        String fileName = UUID.randomUUID().toString();
        File newFile = new File(Contants.IMAGE_PATH + fileName);
        file.transferTo(newFile);
        CompanyBiz companyBiz=(CompanyBiz) MyApplicationContext.getBean("companyBiz");
        fiveCompany.setCompanyLogo(fileName);
        companyBiz.save(fiveCompany);
        return "";
    }
    @RequestMapping("/detail/{companyId}")
    public String detail(@PathVariable("companyId")int companyId,Model model){
        FiveCompany fiveCompany=companyBiz.selectFiveCompanykey(companyId);
//        String companyName=fiveCompany.getCompanyName();
//        List<ThiCase> caseList=thiCaseBiz.findCompany(companyName);
//        model.addAttribute("caseList",caseList);
        model.addAttribute("fiveCompany",fiveCompany);
        return "/fiveCompany/detail";
    }
    @RequestMapping("/deleter/{id}")
    public String deleter(@PathVariable("id")int id){
        return "";
    }
    @RequestMapping("/details/{companyId}")
    public String details(@PathVariable("companyId")int companyId,Model model){
        FiveCompany fiveCompanys=companyBiz.selectFiveCompanykey(companyId);
        model.addAttribute("fiveCompanys",fiveCompanys);
        return "/fiveCompany/details";
    }
}
